<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="公寓名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入公寓名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否有电梯" prop="hasElevator">
        <el-select v-model="queryParams.hasElevator" placeholder="请选择是否有电梯" clearable>
          <el-option
            v-for="dict in dict.type.sys_yesorno"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="物业管理公司" prop="propertyManagementCompany">
        <el-input
          v-model="queryParams.propertyManagementCompany"
          placeholder="请输入物业管理公司"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="设施属性" prop="facilityAttributes">
        <el-select v-model="queryParams.facilityAttributes" placeholder="请选择设施属性" clearable>
          <el-option
            v-for="dict in dict.type.facility_attributes"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="安全属性" prop="securityAttributes">
        <el-select v-model="queryParams.securityAttributes" placeholder="请选择安全属性" clearable>
          <el-option
            v-for="dict in dict.type.security_attribute"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="装修风格" prop="decorationStyle">
        <el-select v-model="queryParams.decorationStyle" placeholder="请选择装修风格" clearable>
          <el-option
            v-for="dict in dict.type.decoration_style"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="最近一次装修时间" prop="lastDecorationDate">
        <el-date-picker clearable
          v-model="queryParams.lastDecorationDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最近一次装修时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最近一次维修时间" prop="lastMaintenanceDate">
        <el-date-picker clearable
          v-model="queryParams.lastMaintenanceDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择最近一次维修时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item label="是否可以养宠物" prop="petPolicy">
        <el-select v-model="queryParams.petPolicy" placeholder="请选择是否可以养宠物" clearable>
          <el-option
            v-for="dict in dict.type.sys_yesorno"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="是否能抽烟" prop="smokingPolicy">
        <el-select v-model="queryParams.smokingPolicy" placeholder="请选择是否能抽烟" clearable>
          <el-option
            v-for="dict in dict.type.sys_yesorno"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="是否有障碍设施" prop="hasAccessibility">
        <el-select v-model="queryParams.hasAccessibility" placeholder="请选择是否有障碍设施" clearable>
          <el-option
            v-for="dict in dict.type.sys_yesorno"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="能源效率等级" prop="energyEfficiency">
        <el-select v-model="queryParams.energyEfficiency" placeholder="请选择能源效率等级" clearable>
          <el-option
            v-for="dict in dict.type.energy_efficiency"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="绿色建筑认证" prop="greenBuildingCertification">
        <el-select v-model="queryParams.greenBuildingCertification" placeholder="请选择绿色建筑认证" clearable>
          <el-option
            v-for="dict in dict.type.sys_yesorno"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['apartmentattribute:apartmentattribute:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['apartmentattribute:apartmentattribute:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['apartmentattribute:apartmentattribute:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['apartmentattribute:apartmentattribute:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="apartmentattributeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="公寓ID" align="center" prop="id" />
      <el-table-column label="公寓名称" align="center" prop="name" />
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="是否有电梯" align="center" prop="hasElevator">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yesorno" :value="scope.row.hasElevator"/>
        </template>
      </el-table-column>
      <el-table-column label="物业管理公司" align="center" prop="propertyManagementCompany" />
      <el-table-column label="设施属性" align="center" prop="facilityAttributes">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.facility_attributes" :value="scope.row.facilityAttributes ? scope.row.facilityAttributes.split(',') : []"/>
        </template>
      </el-table-column>
      <el-table-column label="安全属性" align="center" prop="securityAttributes">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.security_attribute" :value="scope.row.securityAttributes ? scope.row.securityAttributes.split(',') : []"/>
        </template>
      </el-table-column>
      <el-table-column label="装修风格" align="center" prop="decorationStyle">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.decoration_style" :value="scope.row.decorationStyle"/>
        </template>
      </el-table-column>
      <el-table-column label="最近一次装修时间" align="center" prop="lastDecorationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastDecorationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最近一次维修时间" align="center" prop="lastMaintenanceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastMaintenanceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="维修内容" align="center" prop="maintenanceDetails" />
      <el-table-column label="是否可以养宠物" align="center" prop="petPolicy">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yesorno" :value="scope.row.petPolicy"/>
        </template>
      </el-table-column>
      <!-- <el-table-column label="是否能抽烟" align="center" prop="smokingPolicy">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yesorno" :value="scope.row.smokingPolicy"/>
        </template>
      </el-table-column>
      <el-table-column label="是否有障碍设施" align="center" prop="hasAccessibility">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yesorno" :value="scope.row.hasAccessibility"/>
        </template>
      </el-table-column>
      <el-table-column label="能源效率等级" align="center" prop="energyEfficiency">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.energy_efficiency" :value="scope.row.energyEfficiency"/>
        </template>
      </el-table-column>
      <el-table-column label="绿色建筑认证" align="center" prop="greenBuildingCertification">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yesorno" :value="scope.row.greenBuildingCertification"/>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['apartmentattribute:apartmentattribute:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['apartmentattribute:apartmentattribute:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公寓属性对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公寓名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入公寓名称" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否有电梯" prop="hasElevator">
          <el-select v-model="form.hasElevator" placeholder="请选择是否有电梯">
            <el-option
              v-for="dict in dict.type.sys_yesorno"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="物业管理公司" prop="propertyManagementCompany">
          <el-input v-model="form.propertyManagementCompany" placeholder="请输入物业管理公司" />
        </el-form-item>
        <el-form-item label="设施属性" prop="facilityAttributes">
          <el-checkbox-group v-model="form.facilityAttributes">
            <el-checkbox
              v-for="dict in dict.type.facility_attributes"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="安全属性" prop="securityAttributes">
          <el-checkbox-group v-model="form.securityAttributes">
            <el-checkbox
              v-for="dict in dict.type.security_attribute"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="装修风格" prop="decorationStyle">
          <el-select v-model="form.decorationStyle" placeholder="请选择装修风格">
            <el-option
              v-for="dict in dict.type.decoration_style"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="最近一次装修时间" prop="lastDecorationDate">
          <el-date-picker clearable
            v-model="form.lastDecorationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最近一次装修时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最近一次维修时间" prop="lastMaintenanceDate">
          <el-date-picker clearable
            v-model="form.lastMaintenanceDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最近一次维修时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="维修内容" prop="maintenanceDetails">
          <el-input v-model="form.maintenanceDetails" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否可以养宠物" prop="petPolicy">
          <el-select v-model="form.petPolicy" placeholder="请选择是否可以养宠物">
            <el-option
              v-for="dict in dict.type.sys_yesorno"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="是否能抽烟" prop="smokingPolicy">
          <el-select v-model="form.smokingPolicy" placeholder="请选择是否能抽烟">
            <el-option
              v-for="dict in dict.type.sys_yesorno"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否有障碍设施" prop="hasAccessibility">
          <el-select v-model="form.hasAccessibility" placeholder="请选择是否有障碍设施">
            <el-option
              v-for="dict in dict.type.sys_yesorno"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label="能源效率等级" prop="energyEfficiency">
          <el-select v-model="form.energyEfficiency" placeholder="请选择能源效率等级">
            <el-option
              v-for="dict in dict.type.energy_efficiency"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="绿色建筑认证" prop="greenBuildingCertification">
          <el-select v-model="form.greenBuildingCertification" placeholder="请选择绿色建筑认证">
            <el-option
              v-for="dict in dict.type.sys_yesorno"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listApartmentattribute, getApartmentattribute, delApartmentattribute, addApartmentattribute, updateApartmentattribute } from "@/api/apartmentattribute/apartmentattribute";

export default {
  name: "Apartmentattribute",
  dicts: ['energy_efficiency', 'sys_yesorno', 'security_attribute', 'decoration_style', 'facility_attributes'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公寓属性表格数据
      apartmentattributeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        description: null,
        hasElevator: null,
        propertyManagementCompany: null,
        facilityAttributes: null,
        securityAttributes: null,
        decorationStyle: null,
        lastDecorationDate: null,
        lastMaintenanceDate: null,
        maintenanceDetails: null,
        petPolicy: null,
        smokingPolicy: null,
        hasAccessibility: null,
        energyEfficiency: null,
        greenBuildingCertification: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "公寓名称不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公寓属性列表 */
    getList() {
      this.loading = true;
      listApartmentattribute(this.queryParams).then(response => {
        this.apartmentattributeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        description: null,
        hasElevator: null,
        propertyManagementCompany: null,
        facilityAttributes: [],
        securityAttributes: [],
        decorationStyle: null,
        lastDecorationDate: null,
        lastMaintenanceDate: null,
        maintenanceDetails: null,
        petPolicy: null,
        smokingPolicy: null,
        hasAccessibility: null,
        energyEfficiency: null,
        greenBuildingCertification: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公寓属性";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApartmentattribute(id).then(response => {
        this.form = response.data;
        this.form.facilityAttributes = this.form.facilityAttributes.split(",");
        this.form.securityAttributes = this.form.securityAttributes.split(",");
        this.open = true;
        this.title = "修改公寓属性";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.facilityAttributes = this.form.facilityAttributes.join(",");
          this.form.securityAttributes = this.form.securityAttributes.join(",");
          if (this.form.id != null) {
            updateApartmentattribute(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApartmentattribute(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除公寓属性编号为"' + ids + '"的数据项？').then(function() {
        return delApartmentattribute(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('apartmentattribute/apartmentattribute/export', {
        ...this.queryParams
      }, `apartmentattribute_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
